package cn.fl.insurance.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.insurance.condition.InsPrjInfoQueryCondition;
import cn.fl.insurance.service.IInsPlcBscInfoService;
import cn.fl.insurance.service.IInsPlcPrdDtlTkService;
import cn.fl.insurance.vo.InsPrjInfoVO;

/**
 * Function: 续保管理
 * Created by Administrator on 2016/8/17.
 */
@Controller
@RequestMapping("/insurance/insRenwal")
public class InsRenewalController {

	private static final Logger logger = LoggerFactory.getLogger(InsRenewalController.class);

    @Autowired
    private IInsPlcBscInfoService insPlcBscInfoService;
    /**
     * InsPlcPrdDtlTk 服务层BEAN
     */
    @Autowired
    private IInsPlcPrdDtlTkService insPlcPrdDtlTkService;

    /**
     * 到续保管理列表页面
     */
    @RequestMapping(value = "/toListPage.do", method = {RequestMethod.GET})
    public ModelAndView toRenwListPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toRenwListPage", "加载续保列表页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("insurance/insPlcBscInfo/insRenwList");

        //续保状态
        List<KeyValueVO> reneStsCdList=this.insPlcBscInfoService.searchReneStsCdList();
        if(CollectionUtils.isNotEmpty(reneStsCdList)){
            mav.addObject("reneStsCdList", JSON.toJSONString(reneStsCdList));
        }
        return mav;
    }

    /**
     * 初始化列表,续保提醒列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/initList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object initRenwalList(HttpServletRequest request, HttpServletResponse response) {
        String currentPage = request.getParameter("page");
        String pageSize = request.getParameter("pagesize");
        Pager page = new Pager();
        if (StringUtils.isNotEmpty(currentPage)) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }

        if (StringUtils.isNotEmpty(pageSize)) {
            page.setEveryPage(Integer.parseInt(pageSize));
        }

        InsPrjInfoQueryCondition qc=new InsPrjInfoQueryCondition();
        //设置查询条件？
        String cstNm = request.getParameter("cstNm");
        String contNo = request.getParameter("contNo");
        String paySchNo = request.getParameter("paySchNo");
        String plcEndTmMax=request.getParameter("plcEndTmMax");
        String plcEndTmMin=request.getParameter("plcEndTmMin");
        String reneStsCd=request.getParameter("reneStsCd");
        if (StringUtils.isNotEmpty(cstNm)) {
            qc.setCstNm(cstNm);
        }
        if(StringUtils.isNotBlank(contNo)){
            qc.setContNo(contNo);
        }
        if(StringUtils.isNotBlank(paySchNo)){
            qc.setPaySchNo(paySchNo);
        }
        if(StringUtils.isNotBlank(plcEndTmMax)){
          qc.setPlcEndTmMax(plcEndTmMax);
        }
        if(StringUtils.isNotBlank(plcEndTmMin)){
            qc.setPlcEndTmMin(plcEndTmMin);
        }
        if(StringUtils.isNotBlank(reneStsCd)){
            qc.setReneStsCd(reneStsCd);
        }

        qc.setSysCd(CurrentThreadContext.getCurrentSysCd());
        qc.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
        qc.setUsrId(CurrentThreadContext.getCurrentUserId());

        /*List<String> insTypCdList=new ArrayList<>(1);
        insTypCdList.add(InsuranceConstant.CODE_INS_TYP_CD_1);//TODO YJC 商业险？
        qc.setInsTypCdList(insTypCdList);*/

        page.setCondition(qc);

        BaseLogUtils.info(logger, "initList", "初始化续保管理列表页面数据.DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
        Pager pager = this.insPlcBscInfoService.searchRenewalListPage(page);
        List<InsPrjInfoVO> list = (List<InsPrjInfoVO>) pager.getResultList();
        ListPageVO<InsPrjInfoVO> listPage = new ListPageVO<InsPrjInfoVO>(list, pager.getTotalCount());
        return listPage;
    }

    /**
     * 初始化列表,续保提醒产品保单列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/initPrdList.do", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public Object initRenwalPrdList(HttpServletRequest request, HttpServletResponse response) {
        InsPrjInfoQueryCondition qc=new InsPrjInfoQueryCondition();
        //设置查询条件？
        String cstNm = request.getParameter("cstNm");
        String contNo = request.getParameter("contNo");
        String paySchNo = request.getParameter("paySchNo");
        String plcEndTmMax=request.getParameter("plcEndTmMax");
        String plcEndTmMin=request.getParameter("plcEndTmMin");
        String reneStsCd=request.getParameter("reneStsCd");
        String prjId=request.getParameter("prjId");
        if (StringUtils.isNotEmpty(cstNm)) {
            qc.setCstNm(cstNm);
        }
        if(StringUtils.isNotBlank(contNo)){
            qc.setContNo(contNo);
        }
        if(StringUtils.isNotBlank(paySchNo)){
            qc.setPaySchNo(paySchNo);
        }
        if(StringUtils.isNotBlank(plcEndTmMax)){
            qc.setPlcEndTmMax(plcEndTmMax);
        }
        if(StringUtils.isNotBlank(plcEndTmMin)){
            qc.setPlcEndTmMin(plcEndTmMin);
        }
        if(StringUtils.isNotBlank(reneStsCd)){
            qc.setReneStsCd(reneStsCd);
        }
        if(StringUtils.isNotBlank(prjId)){
            qc.setPrjId(Long.valueOf(prjId));
        }

        qc.setSysCd(CurrentThreadContext.getCurrentSysCd());
        qc.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
        qc.setUsrId(CurrentThreadContext.getCurrentUserId());

        /*List<String> insTypCdList=new ArrayList<>(1);
        insTypCdList.add(InsuranceConstant.CODE_INS_TYP_CD_1);//商业险？
        qc.setInsTypCdList(insTypCdList);*/

        BaseLogUtils.info(logger, "initList", "初始化续保管理列表页面数据.DATA:" + JSONObject.toJSONString(qc), CurrentThreadContext.getCurrentUserName());
        List<InsPrjInfoVO> list = this.insPlcBscInfoService.searchRenewalPrdList(qc);
        if(CollectionUtils.isNotEmpty(list)){
            return new ListPageVO<InsPrjInfoVO>(list,list.size());
        }
        return null;
    }
}
